package com.edu.app.controller.core;

import com.alibaba.fastjson.JSON;
import com.edu.app.bean.core.SysListOfValue;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.core.SysActivityService;
import com.edu.app.service.sys.core.SysListOfValueService;
import com.edu.app.util.CommonUtils;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/list_of_value")
@RestController
public class SysListOfValueServiceController {
    @Autowired
    SysListOfValueService sysListOfValueService;

    @Autowired
    SysActivityService sysActivityService;

    @RequestMapping(value = "")
    @RequiresAuthentication
    public ModelMap getListOfValue (){

        try{
            List<SysListOfValue> sysListOfValuesList = sysListOfValueService.getObjectList();
            return MsgUtil.retMsg(SysMessage.GET_ACTIVITY_SUCCEED, StaticConstant.LIST_OF_VALUE, sysListOfValuesList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @RequestMapping(value = "/getListOfValueTypeList")
    @RequiresAuthentication
    public ModelMap getListOfValueTypeList (@RequestParam String type){

        try{
            String type_condition = "%" + type + "%";
            List<String> sysListOfValuesTypeList = sysListOfValueService.getListOfValueTypeList(type_condition);
            return MsgUtil.retMsg(SysMessage.GET_ACTIVITY_SUCCEED, StaticConstant.LIST_OF_VALUE_TYPE, sysListOfValuesTypeList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @RequestMapping(value = "/getListOfValueByType")
    @RequiresAuthentication
    public ModelMap getListOfValueByType (@RequestParam String type){

        try{
            type = "%" + type + '%';
            List<SysListOfValue> sysListOfValuesList = sysListOfValueService.getObjectListByType(type);
            return MsgUtil.retMsg(SysMessage.GET_ACTIVITY_SUCCEED, StaticConstant.LIST_OF_VALUE, sysListOfValuesList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @RequestMapping(value = "/getListOfValueByMulType")
    @RequiresAuthentication
    public ModelMap getListOfValueByMulType (@RequestParam String type){

        try{
            String type_condition = "";
            List<String> typeList = JSON.parseArray(type, String.class);

            for(int i=0; i< typeList.size(); i++){
                if(i == 0){
                    type_condition = "\"" + typeList.get(i) + "\"";
                }else if(i < typeList.size() - 1){
                    type_condition += " or type = \"" + typeList.get(i) + "\"";
                }else{
                    type_condition += " or type = \"" + typeList.get(i) +"\"";
                }
            }

            List<SysListOfValue> sysListOfValuesList = sysListOfValueService.getObjectListByMulType(type_condition);
            return MsgUtil.retMsg(SysMessage.GET_ACTIVITY_SUCCEED, StaticConstant.LIST_OF_VALUE, sysListOfValuesList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping(value = "/{id}")
    @RequiresAuthentication
    public ModelMap getListOfValueById(@PathVariable Integer id){

        try{
            SysListOfValue sysListOfValue  = sysListOfValueService.getObjectById(id);
            return MsgUtil.retMsg(SysMessage.GET_LIST_OF_VALUE_SUCCEED, StaticConstant.LIST_OF_VALUE, sysListOfValue);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping("/pages")
    @RequiresAuthentication
    public ModelMap getListOfValueByPage(@RequestParam Integer offset, @RequestParam Integer limit) {

        try {
            PageInfo pageInfo = sysListOfValueService.findItemByPage(offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_BY_PAGE_SUCCEED, "page_info", pageInfo);
        } catch (Exception e) {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @RequestMapping(value = "/swapItems")
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    @RequiresAuthentication
    public ModelMap swapItems (@RequestParam String item1, @RequestParam String item2){

        try{
            SysListOfValue sysListOfValue1 = JSON.parseObject(item1, SysListOfValue.class);
            SysListOfValue sysListOfValue2 = JSON.parseObject(item2, SysListOfValue.class);
            Integer id = sysListOfValue1.getSequence();
            sysListOfValue1.setSequence(sysListOfValue2.getSequence());
            sysListOfValue2.setSequence(id);
            sysListOfValueService.update(sysListOfValue1);
            sysListOfValueService.update(sysListOfValue2);
            return MsgUtil.retMsg(SysMessage.SWAP_LIST_OF_VALUE_SUCCEED, null, null);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @PostMapping(value = "")
    @RequiresAuthentication
    public ModelMap addListOfValue(@RequestHeader("Authorization") String token, @RequestBody SysListOfValue sysListOfValue){

        try{
            String username = JWTUtil.getUsername(token);
            sysListOfValue.initializeSystemColumns(username);

            Integer r = sysListOfValueService.add(sysListOfValue);

            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_LIST_OF_VALUE_SUCCEED, null, null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.LIST_OF_VALUE_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @PutMapping(value = "")
    @RequiresAuthentication
    public ModelMap update (@RequestHeader("Authorization") String token, @RequestBody SysListOfValue sysListOfValue){

        String username = JWTUtil.getUsername(token);
        sysListOfValue.initializeSystemColumns(username);

        SysListOfValue dbItem = sysListOfValueService.getObjectById(sysListOfValue.getId());
        BeanUtils.copyProperties(sysListOfValue, dbItem, CommonUtils.getNullPropertyNames(sysListOfValue));

        Integer i = sysListOfValueService.update(dbItem);
        if(i > 0 ){
            return MsgUtil.retMsg(SysMessage.UPDATE_LIST_OF_VALUE_SUCCEED, null, null);
        }
        else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }

    @DeleteMapping(value = "")
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    @RequiresAuthentication
    public ModelMap delete (@RequestParam Integer id){
        try{
            sysActivityService.deleteActivityByTypeId(id);
            sysListOfValueService.delete(id);
            return MsgUtil.retMsg(SysMessage.DELETE_LIST_OF_VALUE_SUCCEED, null, null);
        }catch(Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ErrorUtil.retErrMsg(e);
        }
    }
}
